package com.example.summary.leetcode;

import com.example.summary.lianbiao.LianBiaoNode;

import java.util.List;

/**
 * Created by wzn on 2019/6/20.
 */
public class LeetCode141 {

    static class ListNode {
        int val;
        ListNode next;

        public void setNext(ListNode next) {
            this.next = next;
        }

        ListNode(int x, ListNode next) {
            val = x;
            this.next = next;
        }
    }

    public static void main(String[] args) {
        ListNode one = new ListNode(1, null);
        ListNode two = new ListNode(2, null);
        ListNode three = new ListNode(3, null);
        ListNode four = new ListNode(4, new ListNode(5, new ListNode(6, new ListNode(7, three))));
        one.setNext(two);
        two.setNext(three);
//        three.setNext(four);

        System.out.println(hasCycle(one));
    }

    public static boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }

        ListNode one = head;
        ListNode two = head.next;

        while (two != null && two.next != null) {
            if (one == two) {
                return true;
            }

            one = one.next;
            two = two.next.next;
        }

        return false;
    }
}
